#include "mysql_deal_with.h"


int mysql_select_result(char *sqlStr, char *retStr){
	printf("sqlStr : %s\n", sqlStr);
	int ret = 0;
	MYSQL *myconnect = NULL;
	myconnect = mysql_init(myconnect);
	if(myconnect == NULL ){
		printf("can't connect mysql!\n");
		goto err;
	}else{
		if(mysql_real_connect(myconnect, HOST, USER, PASSWORD, DBNAME, 0, NULL, 0) == NULL){
			printf("can't connect db!\n");
			goto err;
		}else{
			printf("connect to database!\n");
		}
	}

	MYSQL_RES *res;
	MYSQL_FIELD *field;
	MYSQL_ROW result_row;
	mysql_query(myconnect, sqlStr);
	res = mysql_store_result(myconnect);

	int col = 0;
	int row = 0;
	int i,j;
	if(res){
		col = mysql_num_fields(res);
		row = mysql_num_rows(res) + 1;
		for(i = 0; ; i++ ){
			field = mysql_fetch_field(res);
			if(field == NULL )break;
			printf("%s ", field->name);
		}
		printf("\n");
		if(row > 1){
			ret = 1;
			for(i = 1 ; i < row ; i++){
				result_row = mysql_fetch_row(res);
				for(j = 0; j < col ; j++ ){
					printf("%s ", result_row[j]);
					if(j == 0 && i == 1)
						strcpy(retStr, result_row[j]);
					else
						strcat(retStr, result_row[j]);
					strcat(retStr, "/");
				}
				printf("\n");
			}
		}
	}

err:
	if(myconnect != NULL)
		mysql_close(myconnect);
	return ret;

}
